from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import cv2

from functools import partial

import qtawesome as qta
import sys


# 公共 设置
class MyBottomView(QListWidget):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.mainwindow = parent


# 大框架

class BottomView(MyBottomView):
    def __init__(self, parent=None):
        super().__init__(parent=parent)

        self.Hlayout = QHBoxLayout()

        self.listWidget = QListWidget()

        icon_size = QSize(30, 30)  # 设置图标的宽度和高度
        self.listWidget.setIconSize(icon_size)

        # 创建带有图标的项并添加到 QListWidget 中
        terminateButton = QListWidgetItem(qta.icon('ei.signal', color='blue'), "")
        self.listWidget.addItem(terminateButton)

        resultViewButton = QListWidgetItem(qta.icon('fa.sticky-note-o', color='red'), "")
        self.listWidget.addItem(resultViewButton)


        #  设置这部分内容大小
        self.listWidget.setGeometry(0,0,140, 200)
        # self.listWidget.setFixedSize(140, 200)
        self.listWidget.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)  # 关闭垂直滚动条


        #  实验阶段  ： 目前先放两个label
        labelxxxx = QLabel()
        labelxxxx.setText("labelxxxxlabelxxxxlabelxxxxlabelxxxxlabelxxxxlabelxxxx")

        #  TODO  双界面

        # TODO 拓展  多界面

        self.label1 = terminate(self)
        #  如果 要有 结果显示  那么， 必须要传入处理的图像

        self.label2 = resultView(self)

        print(self.label1.size())
        print(self.label2.size())

        # self.label1.setGeometry(0, 0, 140, 200)
        # self.label2.setGeometry(0, 0, 140, 200)

        self.Hlayout.addWidget(self.listWidget)
        self.Hlayout.addWidget(self.label1)
        self.Hlayout.addWidget(self.label2)

        self.label2.setVisible(False)

        self.listWidget.clicked.connect(partial(self.click, label1=self.label1, label2=self.label2))

        self.setLayout(self.Hlayout)

    def display1(self,label1, label2):
        label1.setVisible(True)
        label2.setVisible(False)

    def display2(self,label1, label2):
        label1.setVisible(False)
        label2.setVisible(True)

    def click(self, qModelIndex, label1, label2):
        print(qModelIndex)
        print(qModelIndex.row())

        if qModelIndex.row() == 0:
            self.display1(label1, label2)
        else:
            self.display2(label1, label2)


class MyThread(QThread):
    signalForText = pyqtSignal(str)

    def __init__(self,data=None, parent=None):
        super(MyThread, self).__init__(parent)
        self.data = data

    def write(self, text):
        # print(str(text))
        self.signalForText.emit(str(text))  # 发射信号
    #
    # def run(self):
    #     # 演示代码
    #     print("End")

#  具体实现
class terminate(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.init_ui()
        self.th = MyThread()
        self.th.signalForText.connect(self.onUpdateText)
        sys.stdout = self.th



    def onUpdateText(self ,text):
        cursor = self.process.textCursor()
        cursor.movePosition(QTextCursor.End)
        cursor.insertText(text)
        self.process.setTextCursor(cursor)
        self.process.ensureCursorVisible()

    def init_ui(self):

        #1 设置 整体框架  格式：垂直

        self.frameLayout = QVBoxLayout()

        #2 设置进程工作台 组件

        # 2.1 工作台 存在的两个组件  label   button
        self.cmdFrameLabel = QLabel("进程显示：")
        self.cmdFrameLabel.setObjectName('lable') # 不明

        self.clearup_button = QPushButton()
        self.clearup_button.setText("清除")
        self.clearup_button.clicked.connect(self.clearCMD)

        # 2.1 设置进程工作台的 格式  横向
        self.cmdToolLayout = QHBoxLayout()

        # 2.2 将 组件 按序  放入  格式中
        self.cmdToolLayout.addWidget(self.cmdFrameLabel)
        self.cmdToolLayout.addWidget(self.clearup_button)

        # 3 设置 进程  显示  组件

        self.process = QTextEdit(self, readOnly=True)
        self.process.ensureCursorVisible()
        # 用于设置文本的自动换行列或宽度。
        self.process.setLineWrapColumnOrWidth(800)
        self.process.setLineWrapMode(QTextEdit.FixedPixelWidth)
        # self.process.setFixedWidth(500)
        # self.process.setFixedHeight(250)
        # self.process.move(30, 50)


        # 4 将 上述两个组件放入  整体 布局中

        self.frameLayout.addLayout(self.cmdToolLayout)
        self.frameLayout.addWidget(self.process)

        self.setLayout(self.frameLayout)

        # 使用QSS和部件属性美化窗口部件
        # self.widget.setStyleSheet('''
        #     QWidget#widget{
        #         color:#232C51;
        #         background:white;
        #         border-top:1px solid darkGray;
        #         border-bottom:1px solid darkGray;
        #         border-right:1px solid darkGray;
        #     }
        #     QLabel#lable{
        #         border:none;
        #         font-size:16px;
        #         font-weight:700;
        #         font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        #     }
        #     QPushButton#button:hover{border-left:4px solid red;font-weight:700;}
        # ''')

    def clearCMD(self):
        # todo  如果想清空进程显示，则把方法写在这里；
        print("hello")


class resultView(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        print(parent.label1.process.size())
        self.src_img = parent.parent().src_img
        self.init_ui(parent.label1, self.src_img)


    def init_ui(self, label, src_img):

        self.frameLayout = QVBoxLayout()

        self.resultView = QLabel()

        # self.resultView.resize(label.size())

        self.frameLayout.addWidget(self.resultView)

        self.setLayout(self.frameLayout)


        self.displayResult(self.resultView, src_img)
        print(label.size())
        self.setFixedSize(900,100)

    def EndDisplayResult(self, QLabel):
        str = ""
        QLabel.setText(str)


    def displayResult(self, QLabel, src_img):
        rat_A = 10.1

        if src_img is None:
            string_cur_img = "当前没有选中图片"
        elif isinstance(src_img, str):
            string_cur_img = "当前选中图片为：" + src_img

        stringResA = f"A区脱落面积：{rat_A:.4f}"

        QLabel.setText(stringResA)







